<?php
	$username      = htmlspecialchars($_REQUEST['u']);
	$password_hash = md5(htmlspecialchars($_REQUEST['p']));

	switch($_REQUEST['action']) :
	
		case 'list' :
			$request_url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . '/index.php?u='.$username.'&p='.$password_hash.'&list=true';
			$response    = json_decode(file_get_contents($request_url), true);
			break;

		case 'switch' :
			$on  = array();
			$off = array();

			foreach($_REQUEST as $key => $value) :
				if(strpos($key, 'device_') !== false) :
					list(,$device_id) = explode('_', $key);
					if($value == 0) { $off[] = $device_id; }
					if($value == 1) { $on[]  = $device_id; }
				endif;
			endforeach;

			$request_url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . '/index.php?u='.$username.'&p='.$password_hash.'&list=true&on='.implode(',', $on).'&off='.implode(',', $off);
			$response    = json_decode(file_get_contents($request_url), true);
			break;

	endswitch;
?>
<html>
 <head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>SwitchCTRL test</title>
  <style>
	html { height: 100%; background: -webkit-gradient(linear, left bottom, left top, color-stop(0.8, rgb(255,255,255)), color-stop(1, rgb(196,203,255))); background: -moz-linear-gradient(center bottom, rgb(255,255,255) 80%, rgb(196,203,255) 100%); }
	body, input, td, th { font-family: verdana, arial, serif; font-size: 0.8em; text-align: left; }
	input { text-align: center; margin: 2px; font-size: 1em; border: 1px solid rgb(60, 60, 60); }
	input[type="radio"] { border: 0; }
	label { width: 80px; display: block; float: left; margin: 4px 0 0 0; }
  </style>
 </head>
 <body>
  <p>
   <form action="<?=$_SERVER['PHP_SELF']?>" method="post">
    <label for="u">Username:</label> <input type="text" name="u" value="<?=$_REQUEST['u']?>" /><br />
    <label for="p">Password:</label> <input type="password" name="p" value="<?=$_REQUEST['p']?>" /><br />
    <br />
    <input type="submit" value="Get light status" />
    <input type="hidden" name="action" value="list" />
   </form>
  </p>
<?php if(count($response['devices'])) : ?>
  <form action="<?=$_SERVER['PHP_SELF']?>" method="post">
   <table>
    <tr><th>Off</th><th>On</th><th>Lamp</th></tr>
<?php
	foreach($response['devices'] as $device) :
		echo '<tr>';
		echo '<td><input type="radio" value="0"'.($device['status'] == 0 ? ' checked="checked"' : '').' name="device_'.$device['id'].'"></td>'; 
		echo '<td><input type="radio" value="1"'.($device['status'] == 1 ? ' checked="checked"' : '').' name="device_'.$device['id'].'"></td>';
		echo '<td>'.$device['name'].'</td>'; 
		echo "</tr>\n";
	endforeach;
?>
   </table>
   <input type="hidden" name="u" value="<?=htmlspecialchars($_REQUEST['u'])?>" />
   <input type="hidden" name="p" value="<?=htmlspecialchars($_REQUEST['p'])?>" />
   <input type="hidden" name="action" value="switch" />
   <br />
   <input type="submit" value="Switch!" />
  </form>
<?php endif; ?>
<?php
	if($_REQUEST['action'] == 'switch' && $response['success'] == true)
		echo "Ok";
	if($_REQUEST['action'] != '' && $response['success'] == false)
		echo $response['reason'];
?>
 </body>
</html>
